﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        loadHolder();
    }

    /// <summary>
    /// Load usercontrol
    /// Author: ThuanNH
    /// Create: 25/07/2013
    /// </summary>
    private void loadHolder()
    {
        string strUserControlName = clsCommon.fncGetRoute(this.Page, "UserControl");
        //Check usercontrol = kieu phong tro
        if (clsKieuPhongTro.fncCheckExists_KieuPT(strUserControlName) || strUserControlName == basConst.KP_TAT_CA) strUserControlName = "phong-tro";
        clsCommon.fncLoadUserControl(plhContent, this.Page, "UserControl/", strUserControlName, "index");
    }

    /// <summary>
    /// Service lấy danh sách phong tro
    /// </summary>
    /// <param name="iPageIndex"></param>
    /// <param name="iPageSize"></param>
    /// <param name="strCateName"></param>
    /// <param name="iHSX"></param>
    /// <param name="iMH"></param>
    /// <param name="iHDH"></param>
    /// <returns></returns>
    [WebMethod]
    public static string fncLoadMorePhongTro(int iPageIndex, int iPageSize, string strKhuVuc, int iGia, int iDienTich, string strKieuPT, string strUS_ID)
    {
        DataSet dts = clsPhongTro.PhongTro_FND(iPageIndex, iPageSize, strKhuVuc, iGia, iDienTich, "", strKieuPT, strUS_ID,2);
        return clsExportHtml.fncExportPhongTro(dts.Tables[0]);
    }


    /// <summary>
    /// Service lấy danh sách phong tro cho phần quản lý danh sách của user
    /// </summary>
    /// <param name="iPageIndex"></param>
    /// <param name="iPageSize"></param>
    /// <param name="strCateName"></param>
    /// <param name="iHSX"></param>
    /// <param name="iMH"></param>
    /// <param name="iHDH"></param>
    /// <returns></returns>
    [WebMethod]
    public static string fncLoadMorePhongTro_UserManager(int iPageIndex, int iPageSize, string strKhuVuc, int iGia, int iDienTich, string strKieuPT, string strUS_ID)
    {
        DataSet dts = clsPhongTro.PhongTro_FND(iPageIndex, iPageSize, strKhuVuc, iGia, iDienTich, "", strKieuPT, strUS_ID,2);
        return clsExportHtml.fncExportPhongTro_UserManager(dts.Tables[0]);
    }

    /// <summary>
    /// Lấy danh sách tin tức theo chuyên mục
    /// </summary>
    /// <param name="iPageIndex">Trang hiện tại</param>
    /// <param name="iPageSize">Số tin trên 1 trang</param>
    /// <param name="strChuyenMuc">Tên chuyên mục không dấu</param>
    /// <returns></returns>
    [WebMethod]
    public static string fncLoaDMoreTinTuc(int iPageIndex, int iPageSize, string strChuyenMuc)
    {
        DataSet dts = clsTinTuc.TinTuc_FND(iPageIndex, iPageSize, "", "", strChuyenMuc, -1, -1, 0);
        return clsExportHtml.fncExportTinTucDanhSach(dts.Tables[0]);
    }

    /// <summary>
    /// Lấy 5 tin tức nổi bật hiển thị lên trang chủ
    /// Author: NguyenTV
    /// Create Date: 11/09/2013
    /// </summary>
    /// <returns></returns>
    [WebMethod]
    public static string fncLoadTinTuc_NoiBat()
    {
        DataSet dts = clsTinTuc.TinTuc_FND(basConst.CURENT_PAGE, 5, "", "", "", 1, 1, 0);
        return clsExportHtml.fncExportTinTuc_NoiBat(dts.Tables[0]);
    }

    /// <summary>
    /// Lấy tin tức left control
    /// </summary>
    /// <returns></returns>
    [WebMethod]
    public static string fncLoadTinTucLeft(int iNoiBat)
    {
        DataSet dts = clsTinTuc.TinTuc_FND(basConst.CURENT_PAGE, 10, "", "", "", iNoiBat, 1, 0);
        return clsExportHtml.fncExportLeft_NoiBat(dts.Tables[0]);
    }


    /// <summary>
    /// Load tin tức cùng chuyên mục
    /// Author: NguyenTV
    /// Create Date: 05/09/2013
    /// </summary>
    /// <param name="strChuyenMuc"></param>
    /// <returns></returns>
    [WebMethod]
    public static string fncLoadTinTucSame(string strChuyenMuc)
    {
        DataSet dts = clsTinTuc.TinTuc_FND(1, 8, "", "", strChuyenMuc, -1, -1, 0);
        return clsExportHtml.fncExportTinTucSame(dts.Tables[0]);
    }

    /// <summary>
    /// Xu ly dang nhap
    /// Author: ThuanNH
    /// Create Date: 11/09/2013
    /// </summary>
    /// <param name="strEmail"></param>
    /// <param name="strPassword"></param>
    /// <returns></returns>
    [WebMethod]
    public static string fncLogin(string strEmail, string strPassword)
    {
        return clsUsers.fncDoLogin(strEmail, strPassword);
    }

    /// <summary>
    /// Nhận dữ liệu từ clien và insert phản hồi vào csdl
    /// Author: NguyenTV
    /// Create: 13/09/2013
    /// </summary>
    /// <returns></returns>
    [WebMethod]
    public static string fncGuiPhanHoi(string strTitle,string strEmail, string strContent, string strCapcha, string strCapchaInput)
    {
        string strResult = "";
        //return strCapcha + "-" + strCapchaInput;
        clsGopY.stGOPY objGopY = new clsGopY.stGOPY();
        objGopY.strGY_TIEUDE = strTitle;
        objGopY.strGY_EMAIL = strEmail;
        objGopY.strGY_NOIDUNG = strContent;
        objGopY.strGY_IP_CLIENT = clsCommon.fncGetClientIP();
        //Check Capcha;
        if (strCapcha != OsPortal.oStringHelper.rpHash(strCapchaInput)) return "Mã xác nhận không đúng";

        var strError = "";
        if (!clsGopY.GOPY_ACT("A", objGopY, out strError))
        {
            strResult = strError;
        }
        return strResult;
    }

    /// <summary>
    /// Update thông tin Users
    /// </summary>
    /// <param name="strHoTen"></param>
    /// <param name="strEmail"></param>
    /// <param name="strDienThoai"></param>
    /// <param name="strDiaChi"></param>
    /// <param name="blNhanThongBao"></param>
    /// <param name="hdAvarta"></param>
    /// <param name="FuAvarta"></param>
    /// <returns></returns>
    [WebMethod]
    public static string fncUpdateUser(string strHoTen, string strEmail, string strDienThoai
        , string strDiaChi, bool blNhanThongBao, string hdAvarta, string FuAvarta)
    {
        string strResult = "";
        clsUsers.stUSER objUser = new clsUsers.stUSER();
        objUser.strUS_ID = clsUsers.Users_GetValueCooKie(basConst.COOKIE_USER_ID);
        objUser.strUS_EMAIL = strEmail;
        objUser.strUS_HOTEN = strHoTen;
        objUser.strUS_DIENTHOAI = strDienThoai;
        objUser.strUS_DIACHI = strDiaChi;
        objUser.blUS_NHANMAIL = blNhanThongBao;

        string strAvarta_User = hdAvarta;
        //if (FuAvarta.HasFile)
        //{
        //    string strOutImagesName;
        //    OsPortal.oFileHelper.UploadAnhResizeImage(FuAvarta, IMAGES_PATH, widthImages, out strOutImagesName, "", false);
        //    if (!string.IsNullOrEmpty(strOutImagesName))
        //        strAvarta_User = IMAGES_PATH + strOutImagesName;

        //} doi a ty ok
        objUser.strUS_AVATAR = strAvarta_User;

        var strError = "";
        if (!clsUsers.USER_ACT(basConst.MOD_UPDATE, objUser, out strError))
        {
            strResult = strError;
        }

        return strResult;
    }

    /// <summary>
    /// Thay đổi mật khẩu
    /// Author: NguyenTV - 26/09/2013
    /// </summary>
    /// <param name="strOldPass"></param>
    /// <param name="strNewPass"></param>
    /// <returns></returns>
    [WebMethod]
    public static string fncChangePass(string strOldPass, string strNewPass)
    {
        string strResult = "";
        clsUsers.stUSER objUser = new clsUsers.stUSER();
        objUser.strUS_ID = clsUsers.Users_GetValueCooKie(basConst.COOKIE_USER_ID);
        objUser.strUS_OLDPASS = strOldPass;
        objUser.strUS_NEWPASS = strNewPass;

        var strError = "";
        if (!clsUsers.USER_ACT(basConst.MOD_CHANGE, objUser, out strError))
        {
            strResult = strError;
        }

        return strResult;
    }

    /// <summary>
    /// Đăng ký tài khoản
    /// Author: Nguyentv - 04/10/2013
    /// </summary>
    /// <param name="strHoTen"></param>
    /// <param name="strEmail"></param>
    /// <param name="strDienThoai"></param>
    /// <param name="strMatKhau"></param>
    /// <param name="strCapCha"></param>
    /// <param name="strCapchaInput"></param>
    /// <returns></returns>
    [WebMethod]
    public static string fncRegUser(string strHoTen, string strEmail, string strDienThoai, string strMatKhau, string strCapCha
                                    ,string strCapchaInput)
    {
        string strResult = "";
        //Check Capcha;
        if (strCapCha != OsPortal.oStringHelper.rpHash(strCapchaInput))
        {
            strResult = "Mã xác nhận không đúng";
            return strResult;
        }
        clsUsers.stUSER objUser = new clsUsers.stUSER();
        objUser.strUS_HOTEN = strHoTen;
        objUser.strUS_EMAIL = strEmail;
        objUser.strUS_MATKHAU = strMatKhau;
        objUser.strUS_DIENTHOAI = strDienThoai;
        objUser.strUS_MAKICHHOAT = OsPortal.oStringHelper.EncryptStringSHA1(clsCommon.NgauNhien_SoChu(6));
        
        var strError = "";
        if (!clsUsers.USER_ACT(basConst.MOD_ADD, objUser, out strError))
        {
            strResult = strError;
        }
        clsEmail.Send_Mail_Active_Account(objUser.strUS_HOTEN, objUser.strUS_EMAIL, objUser.strUS_MAKICHHOAT);

        return strResult;
    }

    /// <summary>
    /// Cấp lại mật khẩu và gửi về mail
    /// </summary>
    /// <param name="strEmail"></param>
    /// <returns></returns>
    [WebMethod]
    public static string fncGetPass_by_Email(string strEmail)
    {
        string strResult = "";
        string strPassword = clsCommon.NgauNhien_SoChu(6); //Tạo ra mật khẩu ngẫu nhiên;
        clsUsers.stUSER objUer = new clsUsers.stUSER();
        objUer.strUS_EMAIL = strEmail;
        objUer.strUS_MATKHAU = strPassword;

        var strError = "";
        if (!clsUsers.fncNewsPas_by_Email(objUer, out strError))
        {
            strResult = strError;
        }
        clsEmail.Send_Mail_GetPassWord(strEmail, strPassword);
        return strResult;
    }

    /// <summary>
    /// Lấy danh sách lịch sử nạp gold
    /// Author: NguyenTV - 04/10/2013
    /// </summary>
    /// <param name="iPageIndex"></param>
    /// <returns></returns>
    [WebMethod]
    public static string fncGetLogGold(int iPageIndex)
    {
        DataSet dtSLogGold = clsLogGold.LOGGOLD_FND(iPageIndex, basConst.PROC_NUMBER, 0, clsUsers.Users_GetValueCooKie(basConst.COOKIE_USER_ID));
        return clsExportHtml.fncExportLogGold(dtSLogGold.Tables[0]);
    }

    /// <summary>
    /// Reset lượt xem phòng trọ
    /// Author: ThuanNH
    /// Create Date: 11/09/2013
    /// </summary>
    /// <param name="strEmail"></param>
    /// <param name="strPassword"></param>
    /// <returns></returns>
    [WebMethod]
    public static int fncPT_ResetView(string strPT_ID)
    {
        if (clsPhongTro.fncResetLuotXem(strPT_ID)) return 1;
        else return 0;
    }
    /// <summary>
    /// Xóa phòng tọ
    /// Author: ThuanNH
    /// Create Date: 27/09/2013
    /// </summary>
    /// <param name="strPT_ID">Mã phòng trọ</param>
    /// <returns></returns>
    [WebMethod]
    public static string fncPT_Delete(string strPT_ID)
    {
        clsPhongTro.stPhongTro objPhongTro = new clsPhongTro.stPhongTro();
        objPhongTro.PT_ID = strPT_ID;
        return clsPhongTro.fncPhongTro_Act(objPhongTro,basConst.MOD_DELETE);
    }

    /// <summary>
    /// Tắt tin phòng trọ
    /// Author: NguyenTV - 25/10/2013
    /// </summary>
    /// <param name="strPT_ID"></param>
    /// <returns></returns>
    [WebMethod]
    public static string fncPT_OFF_TIN(string strPT_ID)
    {
        return clsPhongTro.fncOFF_TIN(strPT_ID);
    }

    /// <summary>
    /// Bật tin phòng trọ
    /// Author: NguyenTV - 25/10/2013
    /// </summary>
    /// <param name="strPT_ID"></param>
    /// <returns></returns>
    [WebMethod]
    public static string fncPT_ON_TIN(string strPT_ID)
    {
        return clsPhongTro.fncON_TIN(strPT_ID);
    }

    /// <summary>
    /// Up tin len trang chu
    /// </summary>
    /// <param name="strPT_ID"></param>
    /// <returns></returns>
    [WebMethod]
    public static string fncPT_UpTin(string strPT_ID)
    {
        return clsPhongTro.fncUp_Tin(strPT_ID);
    }

    /// <summary>
    /// Lấy danh sách khu vực theo parent ID
    /// </summary>
    /// <param name="strParentID"></param>
    /// <returns></returns>
    [WebMethod]
    public static string fncGetDropKhuVuc_By_ParentID(string strParentID, int iType)
    {
        return clsExportHtml.fncExportKhuVuc_DropDownList(clsKhuVuc.fncGetKhuVucBy_ParentID(strParentID), iType);
    }

    /// <summary>
    /// Lấy Value Hằng số qua ID
    /// </summary>
    /// <param name="strParentID"></param>
    /// <returns></returns>
    [WebMethod]
    public static string fncGetHangSo_ByID(string TV_ID)
    {
        DataTable dt = clsThuVienHangSo.fncGetHangSo_ByID(TV_ID);
        if (dt == null) return "";
        if (dt.Rows.Count <= 0) return "";
        return dt.Rows[0]["tv_value"].ToString();
    }

    /// <summary>
    /// Lấy danh sách vị trí đặt tin VIP
    /// </summary>
    /// <param name="iType"></param>
    /// <returns></returns>
    [WebMethod]
    public static string fncGetDropViTriVip(int iType)
    {
        return clsExportHtml.fncExportViTriVip_DropDownList(clsGiaTinVip.GetAll(), iType);
    }

    /// <summary>
    /// Up tin vip phòng trọ
    /// </summary>
    /// <param name="strPTID"></param>
    /// <param name="strUSID"></param>
    /// <param name="strGVID"></param>
    /// <param name="iSongayhienthi"></param>
    /// <param name="dtNgaybatdauhienthi"></param>
    /// <param name="iGiaTien"></param>
    /// <returns></returns>
    [WebMethod]
    public static string fcnUpTinVip(string strPTID, string strUSID, string strGVID, int iSongayhienthi, string dtNgaybatdauhienthi,
        int iGiaTien)
    {
         string strResult = "";
        var dtNow = DateTime.Now;
        string strStartDate = dtNgaybatdauhienthi+" "+ DateTime.Now.ToString("HH:mm:ss");
        IFormatProvider culture = new CultureInfo("vi-VN", true);
        DateTime startDate = DateTime.ParseExact(strStartDate, "dd/MM/yyyy HH:mm:ss", culture);
       
        clsPhongTro.stPhongTro objUpVip = new clsPhongTro.stPhongTro();
        objUpVip.PT_ID = strPTID;
        objUpVip.US_ID = strUSID;
        objUpVip.GV_ID = strGVID;
        objUpVip.pt_songaydattinvip = iSongayhienthi;
        objUpVip.pt_ngaybatdaudattinvip = startDate;
        objUpVip.pt_giatienVip = iGiaTien;

        string strError;
        if (!clsPhongTro.fncUp_Vip(objUpVip, out strError))
        {
            strResult = strError;
        }
        return strResult;
    }

    /// <summary>
    /// Up Tin phòng trọ tự động
    /// </summary>
    /// <param name="strListData"></param>
    /// <param name="iGold"></param>
    /// <param name="strUSID"></param>
    /// <returns></returns>
    [WebMethod]
    public static string fncAuto_Post(string strListData, int iGold, string strUSID)
    {
        string strResutl = "";
        clsPhongTro.stPhongTro objAutoUp = new clsPhongTro.stPhongTro();
        objAutoUp.LSTDATA_AUTO = strListData;
        objAutoUp.PT_GOLD_AUTO = iGold;
        objAutoUp.US_ID = strUSID;

        string strError;
        if (!clsPhongTro.fncAuto_UpTin(objAutoUp, out strError)) {
            strResutl = strError;
        }
        return strResutl;
    }

}